package com.crazymo.ndk;

import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.crazymo.ndk.jni.JNIHelper;

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final JNIHelper jniHelper = new JNIHelper();
         /*jniHelper.sendBasicType(999.9d,11,8888L,8.88f,false,'M');
        ////jniHelper.sendBasicRefType(Double.valueOf(999.9),new Integer(11),new Long(8888),new Float(8.88),new Boolean(false),new Character('M'));
        String strArr[]={"CrazyMo","JNI"};
        jniHelper.sendString("hello",strArr);
        Blog blog=new Blog("csdn.crazymo_",1008);
        jniHelper.sendBean(blog);

        new DynamicRegistJNI().dynamicRegist();
        new DynamicRegistJNI().dynamicRegist(2008);
        ///jniHelper.sendArrays(new int[]{88,99,66},new Blog[]{new Blog("Crazy",9999)});
        List<String> list=new ArrayList<>();
        list.add("blog.csdn.net");
        list.add("CrazyMo_");
        jniHelper.sendList(list);
        Map<String,String> map=new HashMap<>();
        map.put("m","CrazyMo");
        map.put("mo","csdn");
        jniHelper.sendMap(map);
        */
/*        Log.e("Java","从JNI发送到Java的整形数据"+jniHelper.getBasicData());
        Log.e("Java",jniHelper.getStringData());
        int[] result=jniHelper.getArrayData(4);
        for (int t : result) {
            Log.e("Java", "遍历从JNI发送到Java的数组："+t);
        }

        Blog[] blogArr=jniHelper.getObject();
        Log.e("Java", "返回的对象数组有"+blogArr.length+"个元素");
        for (Blog blog : blogArr) {
            Log.e("Java", "遍历从JNI发送到Java的对象数组："+blog.toString());
        }*/

/*        List<String> list=jniHelper.getList(3);
        Log.e("Java", "返回的List集合有"+list.size()+"个元素");
        for (int i=0;i<list.size();i++){
            Log.e("Java", list.get(i)+i);
        }

        Map<String,String> map=jniHelper.getMap();
        Set<String> keySet=map.keySet();
        Iterator<String> iterator=keySet.iterator();
        while (iterator.hasNext()){
            String key=iterator.next();
            String value=map.get(key);//map根据键获取对应的值
            Log.e("Java", "返回的Map有{key="+key+"value="+value+"}");
        }*/
        //jniHelper.createNativeThread();
        jniHelper.testUnSync();
/*        new Thread(new Runnable() {
            @Override
            public void run() {
                jniHelper.runNativeCallJava(MainActivity.this);
            }
        }).start();*/

        //jniHelper.runNativeCallJava(MainActivity.this);
    }

    public void update(){

        if (Looper.myLooper() == Looper.getMainLooper()){
            Log.e("JavaCall",Thread.currentThread().getName()+"线程被回调");
            Toast.makeText(this,"已经下载完成了",Toast.LENGTH_SHORT).show();
        }else{
            Log.e("JavaCall",Thread.currentThread().getId()+"线程被回调");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    Toast.makeText(MainActivity.this,"已经下载完成了",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
